﻿using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace CLCommuModbus.Config
{
    internal class ObjectConfigOption
    {
        //public int GetCurrentObjectID()
        //{

        //}
        //public string GetCurrentObjectName()
        //{

        //}
        //public int GetObjectCount()
        //{

        //}
        //public int GetCurrentItemName()
        //{

        //}
        //public int GetCurrentItemType()
        //{

        //}
        //public int GetObjectItemCount()
        //{

        //}

        //public bool ItemIsArray()
        //{

        //}
        //public bool ObjectIsArray()
        //{

        //}


        //objectID,Configuration
        internal Dictionary<int, ObjectConfig> objectConfigDict = new Dictionary<int, ObjectConfig>();
        public void SetObject_handShakeMode2(string configFilePath)
        {
            objectConfigDict.Clear();
            string customerJson_TS = File.ReadAllText(configFilePath);
            ////Root
            JObject jb_Customer = JsonConvert.DeserializeObject(customerJson_TS) as JObject;

            foreach (var item in jb_Customer)
            {
                JObject jbitem = JsonConvert.DeserializeObject(item.Value.ToString()) as JObject;

                ObjectConfig objectConfig = new ObjectConfig();

                //遍历子节点Object
                foreach (var item2 in jbitem)
                {
                    List<ObjectItemConfig> objectItemConfigList = new List<ObjectItemConfig>();
                    JObject jbitem1 = JsonConvert.DeserializeObject(item2.Value.ToString()) as JObject;
                    //Object具体内容
                    foreach (var itemObj in jbitem1)
                    {

                        if (itemObj.Key.Contains("#"))
                        {
                            continue;
                        }
                        int startAddress = (int)ObjectHandShakeMode2.StructStartAddress;
                        if (itemObj.Key == "id")
                        {
                            objectConfig.ID = Convert.ToInt32(itemObj.Value);
                        }
                        else if (itemObj.Key == "name")
                        {
                            objectConfig.Name = itemObj.Value.ToString();
                        }
                        else  //存储数据层
                        {
                            JObject jbProperty = JsonConvert.DeserializeObject(itemObj.Value.ToString()) as JObject;
                            ObjectItemConfig configurationItem = new ObjectItemConfig();
                            configurationItem.Name = itemObj.Key;
                            int length = 0;
                            bool isArray = (bool)jbProperty.Property("isArray").Value;
                            foreach (var item3 in jbProperty)
                            {
                                if (item3.Key.Contains("#"))
                                {
                                    continue;
                                }
                                if (item3.Key == "dataType")
                                {
                                    length = TransDataTypeToAddressLenth(item3.Value.ToString());
                                    configurationItem.DataType = (DataType)Enum.Parse(typeof(DataType), item3.Value.ToString().ToUpper());
                                }
                                else if (item3.Key == "arrayLength")
                                {
                                    if (isArray)
                                    {
                                        for (int k = 0; k < Convert.ToInt32(item3.Value); k++)
                                        {
                                            configurationItem.Address.Add(startAddress);
                                            //如果是数组，累加地址
                                            startAddress += length;
                                        }
                                    }
                                    else
                                    {
                                        configurationItem.Address.Add(startAddress);
                                        //如果是数组，累加地址
                                        startAddress += length;
                                    }
                                   
                                }
                            }
                            objectItemConfigList.Add(configurationItem);


                        }

                    }
                    objectConfig.objectItemConfigs = objectItemConfigList;
                    objectConfigDict.Add(objectConfig.ID, objectConfig);
                }

            }
        }
        private int TransDataTypeToAddressLenth(string DataType)
        {
            int info = 0;
            if (DataType.ToLower().Contains("ushort"))
            {
                info = 1;
            }
            if (DataType.ToLower().Contains("bool"))
            {
                info = 1;
            }
            else if (DataType.ToLower().Contains("string"))
            {
                info = 40;
            }
            else if (DataType.ToLower().Contains("float"))
            {
                info = 2;
            }
            else if (DataType.ToLower().Contains("int"))
            {
                info = 2;
            }
            else if (DataType.ToLower().Contains("double"))
            {
                info = 4;
            }
            return info;
        }



    }
}
